package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.ITCommentDao;
import com.rpframework.website.device.domain.Stream;
import com.rpframework.website.device.domain.TComment;
import com.rpframework.website.device.domain.Unit;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.TCommentService;
import com.rpframework.website.device.service.UnitService;
import com.rpframework.website.device.service.UserService;
import com.rpframework.website.device.service.StreamService;

/**
 * 评论
 * @date 2016年3月14日 下午3:49:38
 */
@Controller
@RequestMapping("/api/tcomment")
public class TCommentApiAct extends BaseAct{
	@Resource TCommentService service;
	@Resource UserService userService;
	@Resource UnitService unitService;
	@Resource StreamService StreamService;
	
	public @Resource ITCommentDao iCommentDao;
	/**
	 * 查询评论
	 * @param pager
	 * @param relativeId
	 * @param type
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/list" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String listwineds(
			@RequestParam(value="pager", required=false)Pager<TComment> pager,
			@RequestParam(value="relativeId", required=false)Integer relativeId,
			@RequestParam(value="type", required=false)Integer type, //参数type 
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<TComment>();
		}
		pager.setPageSize(6);//页面固定分页6条
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		if(NumberUtils.isNotValid(relativeId))throw new APICodeException(-6, "relativeId不能为空!");
		if(!NumberUtils.isNotValid(relativeId))pager.getSearchMap().put("relativeId", String.valueOf(relativeId));
	//	if(NumberUtils.isNotValid(type))throw new APICodeException(-6, "type不能为空!");
	
		if(!NumberUtils.isNotValid(type))pager.getSearchMap().put("type", String.valueOf(type));
		pager.getSearchMap().put("state", String.valueOf(1));
		JsonObject json = new JsonObject();
		service.getPager(pager);
		List<TComment> list = pager.getItemList();
		JsonArray arr = new JsonArray();
		json.addProperty("total", pager.getTotalCount());
		for(TComment li : list){
			JsonObject obj = new JsonObject();
			User u = userService.select(li.getUserId());
			if(u==null)continue;
			obj.addProperty("img", TagUtils.getFileFullPath(user.getHeadImg()));
			obj.addProperty("name", u.getNickName());//
			obj.addProperty("time", TagUtils.formatDate(li.getCreateTime()));
			obj.addProperty("content", li.getContent());
			obj.add("urlList", service.getUrlListFormat(li.getUrlList()));
			arr.add(obj);
		}
		json.add("array", arr);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	@RequestMapping(value="/add" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String add(
			@RequestParam(value="relativeId", required=false)Integer relativeId,
			@RequestParam(value="content", required=false)String content,
			@RequestParam(value="urlList", required=false)String urlList,
			@RequestParam(value="type", required=false)Integer type,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		if(StringUtils.isBlank(content)){
			throw new APICodeException(-6, "content不能为空!");
		}
		if(NumberUtils.isNotValid(relativeId)){
			throw new APICodeException(-6, "relativeId不能为空!");
		}
		if(NumberUtils.isNotValid(type)){
			throw new APICodeException(-6, "type不能为空!");
		}
		JsonObject json = new JsonObject();
		Integer size = iCommentDao.queryIntegral(user.getId(),relativeId);//查询我是否可以评论，获取积分
		//如果小于1则没有评论过    评论加积分
		if(size<1){
			//增加积分
			Unit unit = unitService.select(1);//评论一条增加积分数
			Integer userUnit = user.getPoint();//获取当前用户积分
			userUnit+=unit.getPL();//增加积分
			user.setPoint(userUnit);//修改积分
			userService.update(user);//提交数据
			//添加积分增加流水
			Stream Stream = new Stream();
			Stream.setTitle("评论 ");
			Stream.setCreateTime(System.currentTimeMillis()/1000);
			Stream.setUserId(user.getId());
			Stream.setInfo("+"+unit.getPL());
			StreamService.insert(Stream);
		}
		boolean f = service.addComment(relativeId,type,user.getId(),content,urlList);
		if(f) {
			json.addProperty("code", 200);
			json.addProperty("msg", "操作成功");
			json.addProperty("size", size);
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "操作失败");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
		
}
